package com.lin.lazybone.ui;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lin.lazybone.MainActivity;
import com.lin.lazybone.R;
import com.lin.lazybone.application.BaseActivity;
import com.lin.lazybone.entity._User;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by Administrator on 2017/10/22.
 */

public class ForgetActivity extends BaseActivity
{
    private Button surebtn;
    private Button get_validate;
    private EditText tel;
    private EditText validate_num;
    private EditText mail;
    private _User user_data;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toorbal);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        _User bmobuser = new _User();
        actionBar.setDisplayHomeAsUpEnabled(true);
        tel = (EditText) findViewById(R.id.find_tel);
        mail = (EditText) findViewById(R.id.email);
        surebtn = (Button) findViewById(R.id.sureButton);
        get_validate = (Button) findViewById(R.id.get_validate);
        get_validate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BmobSMS.requestSMSCode(tel.getText().toString(), "findPwd", new QueryListener<Integer>() {
                    @Override
                    public void done(Integer smsId, BmobException e) {
                        if (e == null) {//验证码发送成功
                            get_validate.setText("已发送");
                            //设置按钮不可用
                            get_validate.setEnabled(false);
                            //Log.i("smile", "短信id：" + smsId);//用于查询本次短信发送详情
                        }
                    }
                });
            }
        });
        validate_num = (EditText) findViewById(R.id.validate_num);
        surebtn.setOnClickListener(new View.OnClickListener() {
            //验证码确认
            @Override
            public void onClick(View view) {
                BmobSMS.verifySmsCode(tel.getText().toString(), validate_num.getText().toString(), new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        //发送重置密码到邮箱
                        BmobUser.resetPasswordByEmail(mail.getText().toString(), new UpdateListener() {
                            @Override
                            public void done(BmobException e) {
                                if(e==null){

                                    Toast.makeText(ForgetActivity.this,"重置密码请求成功，请到" + mail.getText().toString() + "邮箱进行密码重置操作",Toast.LENGTH_LONG).show();
                                    finish();
                                }else{
                                   Toast.makeText(ForgetActivity.this,"失败:" + e.getMessage(),Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                });

            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }
}



